module.exports = {
    PongProcessName: "Health",
    PongProcessTime: 4500,
    PongProcessSuccess: 0,
    RequestTimeoutCode: -500,
    RequestTimeoutMsg: "timeout",
    RequestDefaultTimeout: 60 * 1000
}

module.exports.conversionBlobToString = (data) => {
    return new Promise((resolve, _) => {
        const reader = new FileReader()
        reader.onload = (e) => {
            resolve(String(e.target.result))
        }
        reader.readAsText(data)
    })
}

// 生成 uid
module.exports.generateMessageUid = () => {
    return 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
        const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    })
}

// 生成 request
module.exports.generateRequest = (uid = this.generateMessageUid()) => {
    return {
        U: uid, // Uid
        P: '',  // Process
        H: {},  // Header
        B: ''   // Body
    }
}

// 生成 response
module.exports.generateResponse = (uid = this.generateMessageUid()) => {
    return {
        U: uid, // Uid
        C: 0,   // Code
        M: '',  // Message
        H: {},  // Header
        B: ''   // Body
    }
}

// 生成 browser context
module.exports.generateBrowserContext = () => {
    return {
        client: null,
        request: null,
        response: null,
    }
}

// 生成 request callback
module.exports.generateRequestCallback = () => {
    return {
        context: null,
        callback: null
    }
}

module.exports.messageBodyEncode = (sign, body) => {
    if (sign === "") {
        return body
    } else {
        let result = ''
        switch (sign) {  // 对body加密
            case "none":
                result = body
                break
            case "base64":
                if (window !== undefined) {
                    result = window.btoa(body)
                } else {
                    // 如果在非浏览器环境运行需要引入第三方库处理  https://github.com/dankogai/js-base64
                    console.log('非浏览器环境运行需要引入第三方库处理 base64')
                }
                break
            case "url":
                result = encodeURIComponent(body)
                break
            default:
                console.error("messageBodyEncode 未知的Encode类型", sign)
                break
        }
        return result
    }
}

module.exports.messageBodyDecode = (sign, body) => {
    if (sign === "") {
        return body
    } else {
        let result = ''
        switch (sign) {  // 对body解密
            case "none":
                result = body
                break
            case "base64":
                if (window !== undefined) {
                    result = window.atob(body)
                } else {
                    // 如果在非浏览器环境运行需要引入第三方库处理  https://github.com/dankogai/js-base64
                    console.log('非浏览器环境运行需要引入第三方库处理 base64')
                }
                break
            case "url":
                result = decodeURIComponent(body)
                break
            default:
                console.error("messageBodyDecode 未知的Decode类型", sign)
                break
        }
        return result
    }
}

// 使用 Promise 配合 async await 实现一个线程休眠任务
module.exports.ThreadSleep = async (time) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve()
        }, time)
    })
}
